package com.roc.web.action.setting;

import java.util.ArrayList;
import java.util.List;

import com.roc.page.BaseSearchCondition;
import com.roc.page.WhereHQLFieldBean;
import com.roc.util.Constance;
import com.roc.util.constant.Role;

public class ClearSearchCondition extends BaseSearchCondition {

	private String mchtCd; // 商户号
	private String mchtName; // 商户名称
	/*
	 * private String stlmDate; // 交易时间(年月日)
	 */
	private String pan; // 卡
	private String termId; // 终端号
	private String opId; // 业务员

	private String minDate; // 交易开始时间(年月日)
	private String maxDate; // 交易结束时间(年月日)

	private String agentId;// 下级代理商
	private String orgCode;
	
	private String merId; // 商户好
	
	private String marketorId;
	
	private String roleType;
	
	private String ucbccdArea;
	
	private String etstranCode;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.BaseSearchCondition#getOrderPartOfHQL()
	 */
	@Override
	protected String getOrderPartOfHQL() {

		return " order by t.STLM_DATE  , t.TRANS_DATE_TIME desc ";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.BaseSearchCondition#getRowCountHQL()
	 */
	@Override
	protected String getRowCountHQL() {
		StringBuffer sb = new StringBuffer();
		
		sb.append( "select count(*) from TBL_CLEAR_TXN t , tbl_mcht_inf m  " );
		if(roleType.equals(Role.AGENT.getCode() )||roleType.equals(Role.SFB_AGENT.getCode() )){
			sb.append(	  " , tbl_mer_agent_relation r "  );
		}
		
						
		sb.append( "  where  t.mcht_cd = m.mcht_cd and t.INS_ID_CD= '"+ orgCode + "' "   );
						
		if(roleType.equals(Role.AGENT.getCode() )||roleType.equals(Role.SFB_AGENT.getCode() )){ 
			sb.append(" and r.agentid = '" + agentId + "' " );
			sb.append(" and t.MCHT_CD = r.merno " );
		}
		if(roleType.equals(Role.MARKET_DIRECT.getCode()) || roleType.equals(Role.MARKET_LEAD.getCode())){
			sb.append(" and exists (select 1 from t_oper_role tor where m.uc_bc_cd_area = tor.agentid and tor.operid ="+marketorId+") ");

		}
		// 如果是商户,查询商户自己的信息
		if( merId != null && !merId.equals("") ){
			sb.append(" and t.MCHT_CD = '" + merId + "'"  ); 
		}
		return sb.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.BaseSearchCondition#getSearchHQL()
	 */
	@Override
	protected String getSearchHQL() {
		StringBuffer sb = new StringBuffer();
		
		sb.append( "select  t.MCHT_CD , t.MCHT_NAME , t.TERM_ID      , t.PAN  , t.STLM_DATE  , t.TRANS_DATE_TIME  , t.TRANS_AMT  , t.MCHT_FEE  , t.MCHT_SET_AMT  , t.MA_SETTLE_DT  , substr(t.remark4, 6, 4) , t.SYS_SER ,t.card_kind_dis"
					+ " from TBL_CLEAR_TXN t , tbl_mcht_inf m  " );
		if(roleType.equals(Role.AGENT.getCode() )||roleType.equals(Role.SFB_AGENT.getCode() )){
			sb.append(	  " , tbl_mer_agent_relation r "  );
		}
		
				
		sb.append( "  where  t.mcht_cd = m.mcht_cd and t.INS_ID_CD= '"+ orgCode + "' "   );
		
		if(roleType.equals(Role.AGENT.getCode() )||roleType.equals(Role.SFB_AGENT.getCode() )){ 
			sb.append(" and r.agentid = '" + agentId + "' " );
			sb.append(" and t.MCHT_CD = r.merno " );
		}
		if(roleType.equals(Role.MARKET_DIRECT.getCode()) || roleType.equals(Role.MARKET_LEAD.getCode())){
			sb.append(" and exists (select 1 from t_oper_role tor where m.uc_bc_cd_area = tor.agentid and tor.operid ="+marketorId+") ");
		}
		// 如果是商户,查询商户自己的信息
		if( merId != null && !merId.equals("") ){
			sb.append(" and t.MCHT_CD = '" + merId + "'" ); 
		}
		
		return sb.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.BaseSearchCondition#getWhereHQLFieldBeanList()
	 */
	@Override
	protected List<WhereHQLFieldBean> getWhereHQLFieldBeanList() {
		List<WhereHQLFieldBean> filedBeanList = new ArrayList<WhereHQLFieldBean>();

		if (Constance.isNotEmpty(mchtCd)) {
			filedBeanList.add(new WhereHQLFieldBean("t.MCHT_CD", mchtCd.trim()
					.trim(), "=", false));
		}
		if (Constance.isNotEmpty(mchtName)) {
			filedBeanList.add(new WhereHQLFieldBean("t.MCHT_NAME", mchtName
					.trim().trim(), "=", false));
		}
		if (Constance.isNotEmpty(opId)) {
			filedBeanList.add(new WhereHQLFieldBean("m.rec_upd_opr", opId
					.trim().trim(), "=", false));
		}
		if (Constance.isNotEmpty(pan)) {
			filedBeanList.add(new WhereHQLFieldBean("t.PAN", pan.trim().trim(),
					"=", false));
		}
		if (Constance.isNotEmpty(termId)) {
			filedBeanList.add(new WhereHQLFieldBean("t.TERM_ID", termId.trim()
					.trim(), "=", false));
		}
		
		if (Constance.isNotEmpty(etstranCode)) {
		    if(!etstranCode.equals("99")){
		    	filedBeanList.add(new WhereHQLFieldBean("substr(t.remark4, 6, 4)", etstranCode, "=", false));	
		    }
		}

		if (Constance.isNotEmpty(minDate) && Constance.isNotEmpty(maxDate)) {
			// Date startDate = Constance.formateDate(minDate);
			// Date endDate = Constance.formateDate(maxDate);
			String startDate = minDate.replace("-", "");
			String endDate = maxDate.replace("-", "");
         	filedBeanList.add(new WhereHQLFieldBean("t.MA_SETTLE_DT >= to_date('"+startDate.trim()+"', 'yyyymmdd')"));
         	filedBeanList.add(new WhereHQLFieldBean("t.MA_SETTLE_DT < to_date('"+endDate.trim()+"', 'yyyymmdd' )+ 1 "));
		}
		
		if(Constance.isNotEmpty(ucbccdArea)){//运维人员
			filedBeanList.add(new WhereHQLFieldBean("m.uc_bc_cd_area", ucbccdArea.trim(), "=", false));
		}
		
		return filedBeanList;
	}

	public String getMchtCd() {
		return mchtCd;
	}

	public void setMchtCd(String mchtCd) {
		this.mchtCd = mchtCd;
	}

	public String getMchtName() {
		return mchtName;
	}

	public void setMchtName(String mchtName) {
		this.mchtName = mchtName;
	}

	public String getPan() {
		return pan;
	}

	public void setPan(String pan) {
		this.pan = pan;
	}

	public String getTermId() {
		return termId;
	}

	public void setTermId(String termId) {
		this.termId = termId;
	}

	public String getMinDate() {
		return minDate;
	}

	public void setMinDate(String minDate) {
		this.minDate = minDate;
	}

	public String getMaxDate() {
		return maxDate;
	}

	public void setMaxDate(String maxDate) {
		this.maxDate = maxDate;
	}

	public String getAgentId() {
		return agentId;
	}

	public void setAgentId(String agentId) {
		this.agentId = agentId;
	}

	public String getOpId() {
		return opId;
	}

	public void setOpId(String opId) {
		this.opId = opId;
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getMerId() {
		return merId;
	}

	public void setMerId(String merId) {
		this.merId = merId;
	}

	public String getMarketorId() {
		return marketorId;
	}

	public void setMarketorId(String marketorId) {
		this.marketorId = marketorId;
	}

	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}

	public String getUcbccdArea() {
		return ucbccdArea;
	}

	public void setUcbccdArea(String ucbccdArea) {
		this.ucbccdArea = ucbccdArea;
	}

	public String getEtstranCode() {
		return etstranCode;
	}

	public void setEtstranCode(String etstranCode) {
		this.etstranCode = etstranCode;
	}

}
